import VueRouter from 'vue-router'
import Vue from 'vue'

import store from '@/store'
import Layout from '@/views/layout/index.vue'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/login',
      component: () => import('@/views/login')
    },
    {
      path: '/',
      redirect: '/dashboard',
      component: Layout,
      children: [
        {
          path: 'dashboard',
          component: () => import('@/views/dashboard/index.vue')
        }
      ]
    }, {
      path: '/branches',
      component: Layout,
      children: [
        {
          path: 'organization-manage',
          component: () => import('@/views/branches/organization-manage.vue')
        },
        {
          path: 'institutions-jobs-area',
          component: () => import('@/views/branches/institutions-jobs-area.vue')
        }
      ]
    }, {
      path: '/transit',
      component: Layout,
      children: [
        {
          path: 'freight-manage',
          component: () => import('@/views/transit/freight-manage.vue')
        },
        {
          path: 'car-models',
          component: () => import('@/views/transit/car-models.vue')
        },
        {
          path: 'vehicle',
          component: () => import('@/views/transit/vehicle.vue')
        },
        {
          path: 'vehicle-detail',
          component: () => import('@/views/transit/components/vehicle-detail.vue')
        },
        {
          path: 'car-register',
          component: () => import('@/views/transit/car-register.vue')
        },
        {
          path: 'driver',
          component: () => import('@/views/transit/driver.vue')
        }
      ]
    }, {
      path: '/branches',
      component: Layout,
      children: [
        {
          path: 'operational-range',
          component: () => import('@/views/branches/operational-range.vue')
        }
      ]
    }
  ]
})
router.beforeEach((to, from, next) => {
  const { token } = store.state.user
  if (to.path !== '/login' && !token) return next('/login')
  next()
})

export default router
